package com.zhuoshi.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhuoshi.bean.Wd_ht_buildModel;
import com.zhuoshi.bean.Wd_ht_projectModel;
import com.zhuoshi.bean.Wd_ht_unitModel;
import com.zhuoshi.service.Wd_ht_buildModelService;
import com.zhuoshi.service.Wd_ht_projectModelService;
import com.zhuoshi.service.Wd_ht_unitModelService;

@Controller
@RequestMapping("/unit")
public class Wd_ht_unitAction {
	@Autowired
	Wd_ht_projectModelService wd_ht_projectModelService ;
	
	@Autowired
	Wd_ht_buildModelService wd_ht_buildModelService ;
	
	@Autowired
	Wd_ht_unitModelService wd_ht_unitModelService ;
	@RequestMapping("/selectAll.do")
	public String selectAll(Wd_ht_unitModel record,Model model){		
		List<Wd_ht_unitModel> wd_ht_unitList = wd_ht_unitModelService.selectAll(record);
		model.addAttribute("wd_ht_unitList", wd_ht_unitList) ;
		return "Wd_ht_unit" ;
	}
	
	@RequestMapping("/Wd_ht_unit_toUpdate.do")
	public String wd_ht_unit_toUpdate(String unitId,Model model){
		Wd_ht_unitModel bean = wd_ht_unitModelService.selectByPrimaryKey(unitId);
		model.addAttribute("bean", bean);
		return "Wd_ht_unit_update" ;
	}
	
	
	
	@RequestMapping("/Wd_ht_unit_update.do")
	public String wd_ht_unit_update(Wd_ht_unitModel record,Model model){	
		wd_ht_unitModelService.updateByPrimaryKeySelective(record) ;
		return "redirect:/unit/selectAll.do" ;
	}
	
	@RequestMapping("/Wd_ht_unit_toAdd.do")
	public String wd_ht_unit_toAdd(Model model){
		List<Wd_ht_projectModel> list = wd_ht_projectModelService.selectAll(null);
		model.addAttribute("list", list);
		return "Wd_ht_unit_add" ;
	}
	
	@RequestMapping("/Wd_ht_unit_add.do")
	public String wd_ht_unit_add(Wd_ht_unitModel record){	
		wd_ht_unitModelService.insertSelective(record);
		return "redirect:/unit/selectAll.do" ;
	}
	
	@RequestMapping("/Wd_ht_unit_delete.do")
	public String wd_ht_unit_delete(String unitId){
		wd_ht_unitModelService.deleteByPrimaryKey(unitId);
		return "redirect:/unit/selectAll.do" ;
	}
	
	@RequestMapping("/getBuild.do")
	public @ResponseBody Map<String,Object> getBuild(String projectId){
		List<Wd_ht_buildModel> Wd_ht_buildList = wd_ht_buildModelService.selectByProjectId(projectId);
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		List list = null;
		if(null!=Wd_ht_buildList && Wd_ht_buildList.size()>0){
			list = new ArrayList();
			for(Wd_ht_buildModel bean:Wd_ht_buildList){
				Map<String,String> taskMap=new HashMap<String,String>();
				taskMap.put("buildId", bean.getBuildId());
				taskMap.put("buildName", bean.getBuildName());
				list.add(taskMap);
			}
		}
		jsonMap.put("tasks", list);
	    return jsonMap;
		
	}
	
	
	@InitBinder
	public void InitBinder(ServletRequestDataBinder binder){
		binder.registerCustomEditor(Date.class, 
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
		
	}
}
